<template>
    <basic-container :btnFlag = true>
        <div class="card-border">
            <process-view-simple-register-flowsheet :taskId="taskId"/>
            <card-header :header-data="headerData"></card-header>
            <activiti-action ref="action"
                             :task-id="taskId"
                             @confirm="submitConfirm"
                             v-model="form">
                <template slot="buttonRight">
                    <web-office-button ref="webOfficeButton"
                                       v-model="form"
                                       type="check"
                                       :listTitle="list.title"
                                       button-text="校对"></web-office-button>
                </template>
            </activiti-action>
            <el-tabs class="bs-tab" v-model="activeName"
                     @tab-click="tabClick"
                     type="card">
                <el-tab-pane label="报批信息"
                             name="first">
                    <bostech-detail :detail-data="listDetail"/>
                    <list-register-info :listId="listId"/>
                </el-tab-pane>
                <el-tab-pane label="批文"
                             name="webOffice">
                    <p>温馨提示：请在新打开的窗口中进行拟文、校对和盖章操作。如果无法进行在线编辑操作，请检查是否已经安装了WebOffice插件。</p>
                    <!--<div>-->
                      <!--<span style="padding-left: 3px;font-weight: 600">批文:</span>-->
                      <!--<div style="display: flex;justify-content: flex-start;align-items : center;">-->
                        <!--<div>-->
                          <!--<preview :attachementId="form.officeFileId" v-if="form.officeFileId" :key="previewShow"></preview>-->
                        <!--</div>-->
                        <!--<div v-if="form.officeFileId">-->
                          <!--<el-button type="primary" icon="el-icon-refresh" circle size="mini" @click="refreshPreview"></el-button>-->
                        <!--</div>-->
                      <!--</div>-->
                    <!--</div>-->
                </el-tab-pane>
                <el-tab-pane label="办理过程"
                             name="second">
                    <process-handle-history :task-id="taskId"/>
                </el-tab-pane>
                <el-tab-pane label="会办流程"
                             name="third">
                    <jointly-handle-div v-if="taskInfo != null && taskInfo.processId != null"
                                        type="JOINTLY_HANDLE_TYPE_LIST_FGW"
                                        :business-key="listId"
                                        :process-id="taskInfo.processId">
                    </jointly-handle-div>
                </el-tab-pane>
            </el-tabs>
        </div>
    </basic-container>
</template>

<script>
    import activitiAction from "@/components/activiti/action";
    import {getbasisinfo} from "@/api/planning/detailList";
    import {taskComplete,getTaskInfo} from "@/api/activiti/monitor";
    import listRegisterInfo from "@/components/list/listRegisterInfo"
    import processHandleHistory from "@/components/activiti/history/processHandleHistory";
    import webOfficeButton from "@/components/web-office/button";
    import {listProcessVariables} from "@/api/activiti/monitor";
    import {getfileinfo,webOfficeCreateSpare} from "@/api/system";
    import * as WebCtrl from "../../../assets/WebOffice";
    import store from '@/store';
    import jointlyHandleDiv from "@/components/activiti/jointly-handle-button/jointlyHandleDiv";

    export default {
        name: "planningListFgwProofread",
        components: {
            activitiAction,
            listRegisterInfo,
            processHandleHistory,
            webOfficeButton,
            jointlyHandleDiv
        },
        data(){
            return {
                headerData:{
                    title:'主办经办校对'
                },
                activeName: 'first',
                taskId: null,
                listId: null,
                list: null,
                listDetail: [],
                form:{
                },
                formRules: {

                },
                previewShow:'',
                taskInfo: null
            };
        },
        created() {
            this.init();
        },
        watch: {
          'activeName'(val) {
            if(val == 'webOffice'){
              this.toWebOfficeCheck()
            }
          }
        },
        methods: {
            init(){
                this.taskId = this.$route.params.taskId;
                this.listId = this.$route.query.listId;
                this.getList();
                this.getProcessVariables();
                this.getTaskInfo();
            },
            getTaskInfo() {
                getTaskInfo(this.taskId).then((res) => {
                    this.taskInfo = res.data.data;
                });
            },
            getList() {
                getbasisinfo(this.listId).then((res) => {
                    this.list = res.data.data;
                    let list = this.list;
                    if(list != null){
                        this.listDetail = [
                            {label: '清单标题', value: list.title },
                            {label: '清单说明', value: list.description}
                        ]
                    }
                });
            },
            getProcessVariables(){
                listProcessVariables({taskId: this.taskId}).then((res) => {
                    let processVariables = res.data.data;
                    if (processVariables != null) {
                        this.form.officeFileId = processVariables.officeFileId;
                    }
                });
            },
            goBack () {
                this.$router.go(-1);
            },
            submitConfirm(validActionForm, stopLoading, closeDialog) {
                validActionForm((actionFormValid, object) => {
                    this.validateForm((valid) => {
                        if (valid && actionFormValid) {
                            let params = this.form;
                            taskComplete(this.taskId, params).then(response => {
                                if (response.data.code == 0) {
                                    this.$notify({
                                        title: '成功',
                                        message: '提交成功',
                                        type: 'success',
                                        duration: 2000
                                    });
                                    closeDialog();
                                    this.goBack();
                                }
                            }).finally(() => {
                                stopLoading();
                            });
                        } else {
                            this.formValidateErrorNotify();
                            stopLoading();
                        }
                    })
                });
            },
            formValidateErrorNotify () {
                this.$notify({
                    title: '错误',
                    message: '表单存在错误，请按提示修改',
                    type: 'error',
                    duration: 2000
                });
            },
            validateForm (callback) {
                if (this.$refs.form != null) {
                    this.$refs.form.validate((valid, object) => {
                        callback(valid, object);
                    });
                } else {
                    if (callback != null) {
                        callback(true);
                    }
                }
            },
            tabClick(tab){
                if(tab.name == 'webOffice'){
                    this.$refs.webOfficeButton.clickBtn();
                }
            },
            refreshPreview(){
              this.previewShow = new Date().getTime();
            },
            toWebOfficeCheck() {
              getfileinfo(this.form.officeFileId).then((res) => {
                let fileInfo = res.data.data;
                if (fileInfo != null && fileInfo.size > 0) {
                  WebCtrl.ShowPage(store.getters.access_token, 'check', fileInfo.fileId, fileInfo.fileName,null,this.list.title);
                } else {
                  this.toWebOfficeCreate((fileId) => {
                    this.form.officeFileId = fileId;
                  })
                }
              });
            },
            toWebOfficeCreate() {
              this.webOfficeCreateSpare((fileId) => {
                this.form.officeFileId = fileId;
                //拟稿：create   核稿:check  签章：signature
                WebCtrl.ShowPage(store.getters.access_token, 'create', fileId, null, null, this.list.title, this.list.code);
              });
            },
            webOfficeCreateSpare(callback) {
              webOfficeCreateSpare().then(response => {
                if (response.data.code == 0) {
                  let fileId = response.data.data;
                  callback(fileId);
                }
              })
            },
        }
    }
</script>

<style lang="scss" scoped>
    .collapse{
        margin-top:2rem;
    }
</style>
